package com.jiangli.controller.user;


import com.jiangli.annotation.RateLimit;
import com.jiangli.dto.doubao.ChatRequestDTO;
import com.jiangli.entity.doubao.Message;
import com.jiangli.service.DoubaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import reactor.core.Disposable;

import javax.print.DocFlavor;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 豆包控制器，处理前端请求并返回流式响应
 */
@RestController
@RequestMapping("/doubao")
public class DoubaoController {

    @Autowired
    private DoubaoService doubaoService;

    /**
     * 处理流式对话请求
     */
    @PostMapping("/stream")
    @RateLimit
    public ResponseBodyEmitter streamChat(@RequestBody ChatRequestDTO request) {
        // 创建响应发射器，设置30秒超时
        ResponseBodyEmitter emitter = new ResponseBodyEmitter(30000L);
        // 从请求中获取参数
        String prompt = request.getPrompt();
        List<Message> history = request.getHistory();
        List<Long> ids = request.getIds();
        // 调用豆包服务获取流式响应
        if(ids != null && !ids.isEmpty()) {
            //当菜品列表里油脂时候调用关联菜品的api
            Disposable disposable = doubaoService.generateStreamWithFood(prompt, history,ids)
                    .subscribe(
                            // 处理每个文本片段，发送给前端
                            chunk -> {
                                try {
                                    emitter.send(chunk);
                                } catch (IOException e) {
                                    emitter.completeWithError(e);
                                }
                            },
                            // 处理错误
                            error -> emitter.completeWithError(error),
                            // 完成时关闭响应
                            () -> emitter.complete()
                    );

            // 响应完成或出错时，取消订阅释放资源
            emitter.onCompletion(disposable::dispose);
            emitter.onError((e) -> disposable.dispose());
        }
        else {
            //当菜品列表没有值的时候
            Disposable disposable = doubaoService.generateStream(prompt, history)
                    .subscribe(
                            // 处理每个文本片段，发送给前端
                            chunk -> {
                                try {
                                    emitter.send(chunk);
                                } catch (IOException e) {
                                    emitter.completeWithError(e);
                                }
                            },
                            // 处理错误
                            error -> emitter.completeWithError(error),
                            // 完成时关闭响应
                            () -> emitter.complete()
                    );

            // 响应完成或出错时，取消订阅释放资源
            emitter.onCompletion(disposable::dispose);
            emitter.onError((e) -> disposable.dispose());
        }
        return emitter;
    }


}
